package com.lelefly.base.serviceImp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.lelefly.base.Validator.FarmlandAccessValidator;
import com.lelefly.base.daoimp.BaseDao;
import com.lelefly.base.model.Result;
import com.lelefly.base.model.ValidateErrors;
import com.lelefly.base.po.FarmlandAccess;
import com.lelefly.base.po.FarmlandInfo;
import com.lelefly.constant.Constant;
import com.lelefly.handleexecption.ServiceExecption;
import com.lelefly.handleexecption.ValidateExecption;

@Service
public class FarmlandAccessService extends BaseService {

	@Resource
	public BaseDao baseDao;
	
	@Resource
	public FarmlandAccessValidator farmlandAccessValidator;
	
	
	@SuppressWarnings({ "null", "rawtypes" })
	public Result getFarmlandAccesss(String user) {
		Result<FarmlandAccess> result = new Result<>();
		try {
			if (StringUtils.isEmpty(user) ) {
				throw new ServiceExecption("参数错误");
			}
			String hql = "select FarmlandAccess where user='"+user+"' ";
			List<FarmlandAccess> farmlandAccesses= baseDao.queryObjetReList(hql);
			result.setState(Constant.HTTP_STATUS_OK);
			result.setResultList(farmlandAccesses);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ServiceExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
	
	@SuppressWarnings("null")
	public Result<FarmlandAccess> addFarmlandAccess(FarmlandAccess farmlandAccess) {
		Result<FarmlandAccess> result = new Result<>();
		try {
			ValidateErrors validateErrors = new ValidateErrors();
			farmlandAccessValidator.validator(farmlandAccess, validateErrors);
			if (validateErrors.isHasError()) {
				throw new ValidateExecption(validateErrors.getStackFirstError());
			}
			String sql ="from FarmlandAccess as model where usernm ='"
			               +farmlandAccess.getUsernm()+"' farmlandid='"
					       +farmlandAccess.getFarmlandid()+"'";
			FarmlandInfo fInfo = (FarmlandInfo) baseDao.getObjcetReUnique(sql);
			if (fInfo!=null) {
				throw new ValidateExecption("已对该农田提交授权申请");
			}
			baseDao.saveObject(farmlandAccess);
			result.setState(Constant.HTTP_STATUS_OK);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ValidateExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
	
	@SuppressWarnings("null")
	public Result<FarmlandAccess> updateFarmlandInfo(FarmlandAccess farmlandAccess) {
		Result<FarmlandAccess> result = new Result<>();
		try {
			ValidateErrors validateErrors = new ValidateErrors();
			farmlandAccessValidator.validator(farmlandAccess, validateErrors);
			if (validateErrors.isHasError()) {
				throw new ValidateExecption(validateErrors.getStackFirstError());
			}
			baseDao.UpdateObject(farmlandAccess);
			result.setState(Constant.HTTP_STATUS_OK);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ValidateExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
	
	@SuppressWarnings("null")
	public Result delFarmlandAccess(String id ) {
		Result<FarmlandAccess> result = new Result<>();
		try {
			if (StringUtils.isEmpty(id)) {
				throw new ServiceExecption("删除条件为空");
			}
			String sql = "detele FarmlandAccess as model where id='"+id+"' "; 
			baseDao.deteleObjectbySql(sql);
			result.setState(Constant.HTTP_STATUS_OK);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ServiceExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
}
